package com.example.cms.service.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.cms.dao.CustomerDao;
import com.example.cms.entity.Customer;
import com.example.cms.service.CustomerService;

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService{
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	CustomerService cusSer;
	
	@Override
	public List<Customer> getAllCus() {
		// TODO Auto-generated method stub
		return customerDao.getAllCus();
	}

	@Override
	public List<Customer> getByPageCus(Integer page) {
		int pagesize = cusSer.countPageSize();//得到总页码数
		List<Customer> list = new ArrayList<Customer>();
		if(page > pagesize) {
			System.out.println("页码过大");
		}else {
			System.out.println("页码合适");
			System.out.println(page);
			list = customerDao.getAllCusBySize(0,pageSize);
		}
		return list;
	}

	@Override
	public Integer addCustomer(Customer customer) {
		return customerDao.addCus(customer);
	}

	@Override
	public Integer updateCustomer(Customer customer) {
		return customerDao.updateCus(customer);
	}

	@Override
	public List<Customer> LikeName(String customerName) {
		return customerDao.selectLikeCusName(customerName);
	}


	@Override
	public List<Customer> getAllCusAndOr() {
		return customerDao.getAllCusAndOr();
	}

	@Override
	public  Integer countPageSize() {
		int size = customerDao.getDataNum();
		int pageNum = 0;//页码数
		if(size % pageSize == 0) {
			pageNum = size / pageSize;
		}else {
			pageNum = size / pageSize + 1;
		}
		return pageNum;
	}

}
